#include "StdAfx.h"
#include "recent_chat_op.h"
#include <Util/StringUtil.h>
#include <interface/data_op/table_wrapper.h>

namespace aot{ namespace data{

recent_chat_op_impl::recent_chat_op_impl(void)
{
}

recent_chat_op_impl::~recent_chat_op_impl(void)
{
}

bool 
recent_chat_op_impl::del( const char* session_id )
{
	std::string sql_fmt = "delete from recent_chat_info where session_id='%s';";

	std::string sql_exe;
	sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), session_id);

	return g_db_user->exec(sql_exe.c_str());
}

bool 
recent_chat_op_impl::del_all()
{
    std::string sql_exe = "delete from recent_chat_info;";
    return g_db_user->exec(sql_exe.c_str());
}

bool 
recent_chat_op_impl::insert_recent_chat_info(irecent_chat_info* p)
{
    std::string str_session_id = p->session_id->c_str();
    std::string str_time = p->time->c_str();
    
    remove_sin_quot(str_session_id);
    remove_sin_quot(str_time);

    std::string sql_fmt = 
        "insert into recent_chat_info \
        (session_id, \
        time, \
        chat_type) \
        values\
        ('%s', '%s', %d);";

    std::string sql_exe;

    sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(),
        str_session_id.c_str(),        
        str_time.c_str(),      
        p->chat_type
        );
    return g_db_user->exec(sql_exe.c_str());
}

bool 
recent_chat_op_impl::get_recent_chat_info_list(isqlite3_table** out)
{
    std::string sql_exe;
    sql_exe = "select session_id, time, chat_type from recent_chat_info;";
    return g_db_user->exec_and_get_record(sql_exe.c_str(), out);
}


}} /// end namespace aot/data
